import { Component, Input, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { DataSharingService } from 'src/app/@core/services/dataSharing/data-sharing.service';
import { PurchaseService } from 'src/app/@core/services/purchase/purchase.service';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { AttachmentComponent } from 'src/app/@shared/components/attachment/attachment.component';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-select-require',
  templateUrl: './select-require.component.html',
  styleUrls: ['./select-require.component.scss'],
})
export class SelectRequireComponent {
  @Input() data: any;
  fifter = {
    PageSize: 20,
    PageNumber: 1,
    StartTime: '',
    EndTime: '',
    PurchaseRequiredNum: '',
    PartNumPartName: '',
    PurchaseManNumPurchaseManName: '',
    Status: '审核通过',
    isClosed: false,
    ZhuanHuanWanDe: false,
  };
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20,
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  tableData = [];
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '采购需求单号',
        fieldType: 'text',
        fixedLeft: '80px',
      },
      {
        field: '申请人',
        header: '申请人',
        fieldType: 'text',
      },
      {
        field: '申请部门',
        header: '申请部门',
        fieldType: 'text',
      },
      {
        field: '建议采购日期',
        header: '建议采购日期',
        fieldType: 'text',
      },
      {
        field: '物料编号',
        header: '物料编号',
        fieldType: 'text',
      },
      {
        field: '图片',
        header: '图片',
        fieldType: 'text',
      },
      {
        field: '物料名称',
        header: '物料名称',
        fieldType: 'text',
      },
      {
        field: '规格型号',
        header: '规格型号',
        fieldType: 'text',
      },
      {
        field: '物料单位',
        header: '物料单位',
        fieldType: 'text',
      },
      {
        field: '申请采购数量',
        header: '申请采购数量',
        fieldType: 'text',
      },
      {
        field: '已订购数量',
        header: '已订购数量',
        fieldType: 'text',
      },
      {
        field: '可定数量',
        header: '可定数量',
        fieldType: 'text',
      },
      {
        field: '建议供应商',
        header: '建议供应商',
        fieldType: 'text',
      },
      {
        field: '是否检验',
        header: '是否检验',
        fieldType: 'text',
      },
      {
        field: '备注',
        header: '备注',
        fieldType: 'text',
      },
      {
        field: '制单人',
        header: '制单人',
        fieldType: 'text',
      },
      {
        field: '制单日期',
        header: '制单日期',
        fieldType: 'text',
      },
    ],
  }; //表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px',
    },
    {
      field: '采购需求单号',
      width: '200px',
    },
    {
      field: '申请人',
      width: '150px',
    },
    {
      field: '申请部门',
      width: '170px',
    },
    {
      field: '建议采购时间',
      width: '170px',
    },
    {
      field: '物料编号',
      width: '150px',
    },
    {
      field: '图片',
      width: '150px',
    },
    {
      field: '物料名称',
      width: '150px',
    },
    {
      field: '规格型号',
      width: '150px',
    },
    {
      field: '物料单位',
      width: '150px',
    },
    {
      field: '申请采购数量',
      width: '150px',
    },
    {
      field: '已订购数量',
      width: '150px',
    },
    {
      field: '可定数量',
      width: '150px',
    },
    {
      field: '建议供应商',
      width: '150px',
    },
    {
      field: '是否检验',
      width: '150px',
    },
    {
      field: '备注',
      width: '150px',
    },
    {
      field: '制单人',
      width: '150px',
    },
    {
      field: '制单日期',
      width: '150px',
    },
  ]; //表格宽度配置
  msgs: { severity: any; summary: any; content: any }[];
  @ViewChild(DataTableComponent, { static: true })
  datatable: DataTableComponent;
  checkedIds: any = [];
  date = [];
  status = ['待审核', '审核通过', '审核不通过'];
  filterRows: any[]=[];
  constructor(
    private SharedMethodService: SharedMethodService,
    private PurchaseService: PurchaseService
  ) {}
  ngOnInit(): void {
    this.filter();
  }
  filter() {
    this.PurchaseService.getPurchaseRequirementPart(this.fifter).subscribe(
      (data: any) => {
        const filterIds = this.data.purchaseRequiredOrderPartIds
        const filteredItems = data.items.filter((item: any) => item.purchaseQuantity - item.orderedQuantity > 0); // 过滤掉 purchaseQuantity - orderedQuantity <= 0 的项
        this.tableData = data.items.map((item: any) => {
          if (filterIds.includes(item.purchaseRequiredOrderPartId)) {
            return {
              ...item,
              $checked: true,
              $halfChecked: false,
            };
          }
          return item;
        });
        this.pager.total = data.totalCount;
        this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
      }
    );
  }
  filter2() {
    this.fifter.PageNumber =1
    this.filter()
  }
  reset() {
    this.fifter = {
      PageSize: 20,
      PageNumber: 1,
      StartTime: '',
      EndTime: '',
      PurchaseRequiredNum: '',
      PartNumPartName: '',
      PurchaseManNumPurchaseManName: '',
      Status: '审核通过',
      isClosed: false,
      ZhuanHuanWanDe: false,
    };
    this.date = []
    this.filter();
  }
  onRowCheckChange(
    checked: any,
    rowIndex: any,
    nestedIndex: any,
    rowItem: { $checked: any; $halfChecked: boolean }
  ) {
    this.SharedMethodService.onRowCheckChange(
      checked,
      rowIndex,
      nestedIndex,
      rowItem,
      this.datatable
    );
  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(
      this.datatable,
      'purchaseRequiredOrderId'
    ).subscribe((data: any) => {
      this.checkedIds = data;
    });
  } //获取选中行
  CheckedRows = [];
  getCheckedRowsData() {
    this.SharedMethodService.getCheckedRowsData(this.datatable).subscribe(
      (data: any) => {
          const filterIds = this.data.purchaseRequiredOrderPartIds; // 确保这是一个数组
          this.CheckedRows = data;
          this.tableData = this.tableData.map((item: any) => {
            if (filterIds.includes(item.purchaseRequiredOrderPartId)) {
              return {
                ...item,
                $checked: true,
                $halfChecked: false
              };
            }
            return item;
          });
          this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
          this.filterRows = this.CheckedRows.filter((item: any) => !filterIds.includes(item.purchaseRequiredOrderPartId));
      }
    );
  }
  cleared() {
    this.reset()
  } //清空选中行
  pageIndexChange($event: any) {
    this.filter();
  } //切换页码
  pageSizeChange($event: any) {
    this.filter();
  } //改变每页显示数量
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  } //提示框
  getImagesArray(fileURL: string): string[] {
    let urls = fileURL.split(',');
    urls = urls.map((url) => environment.modifiedApiUrl + url);
    return urls;
  }
  dateChange($event: any) {
      this.fifter.StartTime = this.date[0].toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/')
      this.fifter.EndTime = this.date[1].toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/')
  }
}
